package uml.behavioral.observer.ext;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.HashSet;

/**
 * on 2023/1/4 16:57
 */
public abstract class Light {
    protected int lightTime;
    protected String state;
    protected HashSet<Driver> drivers;

    public int getLightTime() {
        return lightTime;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public void setLightTime(int lightTime) {
        this.lightTime = lightTime;
    }

    public Light() {
        this.drivers = new HashSet<>();
    }

    public void willBeObservedBy(Driver driver) {
        if (!driver.isPassed() && !drivers.contains(driver)) {
            drivers.add(driver);
        }
    }

    public void hasBeenObservedBy(Driver driver) {
        if (driver.isPassed()) {
            drivers.remove(driver);
        }
    }

    protected abstract void notice();

    public abstract void changeState() throws ParserConfigurationException, IOException, SAXException, InterruptedException;

    @Override
    public String toString() {
        return "信号灯[ " +
                "剩余：" + lightTime +
                "秒 颜色：" + state + " ]";
    }
}
